package dao;

import entity.MontantCaisse;
import entity.Vente;
import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import static org.hibernate.criterion.Example.create;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class VenteDAO extends BaseHibernateDAO {

    private static final Logger log = LoggerFactory.getLogger(VenteDAO.class);

    public void save(Vente transientInstance) {
        log.debug("saving Vente instance");
        try {
            getSession().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void evictMe(Vente v) {
        getSession().evict(v);
    }

    public void delete(Vente persistentInstance) {
        log.debug("deleting Vente instance");
        try {
            getSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public Vente findById(java.lang.Integer id) {
        log.debug("getting Vente instance with id: " + id);
        try {
            Vente instance = (Vente) getSession().get("entity.Vente", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List<Vente> findByExample(Vente instance) {
        log.debug("finding Vente instance by example");
        try {
            List<Vente> results = (List<Vente>) getSession()
                    .createCriteria("entity.Vente").add(create(instance))
                    .list();
            log.debug("find by example successful, result size: "
                    + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    public List findByProperty(String propertyName, Object value) {
        log.debug("finding Vente instance with property: " + propertyName
                + ", value: " + value);
        try {
            String queryString = "from Vente as model where model."
                    + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List findAll() {
        log.debug("finding all Vente instances");
        try {
            String queryString = "from Vente where prix_achat<>0";
            Query queryObject = getSession().createQuery(queryString);
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }
     public List findByEvent(int id) {
        log.debug("finding all Vente instances");
        try {
            String queryString = "from Vente where id_event="+id;
            Query queryObject = getSession().createQuery(queryString);
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List findByGuichet(Integer id) {
        log.debug("finding all Vente instances");
        try {
            String queryString = "from Vente where id_gichet=" + id + " and prix_achat<>0 ORDER BY id DESC";
            Query queryObject = getSession().createQuery(queryString);
            return queryObject.setMaxResults(10).list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public Vente merge(Vente detachedInstance) {
        log.debug("merging Vente instance");
        try {
            Vente result = (Vente) getSession().merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Vente instance) {
        log.debug("attaching dirty Vente instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(Vente instance) {
        log.debug("attaching clean Vente instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public List<Vente> findByLastAndGuichet(MontantCaisse seleMontantCaisse) {
        
        log.debug("finding all Vente instances");
		try {
			String queryString = "from Vente where id>"+seleMontantCaisse.getLast()+" and id_gichet="+seleMontantCaisse.getGuichet().getId()+" and prix_achat<>0 order by id DESc ";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
    }
}